<?php

class BetTotal_sb
{

    public $disk = "a";
    public $lotto_id = 0;
    public $table_name = "bettotal_sb_0";
    public $db;
    public $tbl;
    public $betXYArr;

    public function BetTotal_sb($lotto_id = "", $gametype = 25)
    {
        global $g_gametype_3d;
        global $g_betXYArr;
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->betXYArr = array();
        $this->db = new DB();
        $tempTblName = "bettotal_{$this->tbl}_" . $this->getPaperOpen($lotto_id);
        if ($this->chkTbl($tempTblName))
        {
            $this->table_name = $tempTblName;
        }
        else
        {
            $this->table_name = "bettotal_{$this->tbl}_0";
        }
    }

    public function getDataListTotal($lotto_id, $bettypeStr, $showid, $split = 0, $diskStr = "", $xyid = -1)
    {
        $reArr = array ();
        $betArr = explode("_", $bettypeStr);
        $subid = 0;
        $bettype = $betArr[0];
        if ($split == 0)
        {
            $amountstr = "amount1" . $diskStr;
            $amountstr2 = "amount3" . $diskStr;
            $back1str = "back1" . $diskStr;
        }
        else
        {
            $amountstr = "amount2" . $diskStr;
            $amountstr2 = "amount4" . $diskStr;
            $back1str = "back3" . $diskStr;
        }
        $sql = "SELECT sum(totalnum{$diskStr}) as totalnum , sum({$amountstr}) as amount, sum({$amountstr2}) as amount_rate, sum({$back1str}) as back1, sum(back2{$diskStr}) as back2, MAX(updatetime) as updatetime FROM {$this->table_name}";
        $sql .= " WHERE user_id='{$showid}' AND bettype='{$bettype}' AND subid='{$subid}' AND lotto_id='{$lotto_id}'";
        $sql .= " GROUP BY lotto_id, bettype";
        $sql .= " ORDER BY amount DESC ";
        $result = $this->db->query($sql);
        if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            return $row;
        }
        return false;
    }

    public function getDataListTotalForBettypeS($lotto_id, $bettype, $showid, $split = 0, $diskStr = "", $xyid = -1, $chkSubID = 0)
    {
        $reArr = array ();
        if ($split == 0)
        {
            $amountstr = "amount1" . $diskStr;
            $amountstr2 = "amount3" . $diskStr;
        }
        else
        {
            $amountstr = "amount2" . $diskStr;
            $amountstr2 = "amount4" . $diskStr;
        }
        if (0 < $chkSubID)
        {
            $chkSubIDStr = " AND subid = {$chkSubID} ";
        }
        else
        {
            $chkSubIDStr = "";
        }
        $sql = "SELECT sum(totalnum{$diskStr}) as totalnum , sum({$amountstr}) as amount, MAX(updatetime) as updatetime FROM {$this->table_name}";
        $sql .= " WHERE user_id = {$showid} AND bettype IN ({$bettype}) AND lotto_id = {$lotto_id} {$chkSubIDStr}";
        $sql .= " GROUP BY lotto_id";
        $result = $this->db->query($sql);
        if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            return $row;
        }
        return false;
    }

    public function getDataList($lotto_id, $bettypeStr, $xyid, $number, $showid, $amount = 0, $ratemod = 2, $mstFlag = false)
    {
        $betArr = explode("_", $bettypeStr);
        if (isset($betArr[1]))
        {
            $subid = $betArr[1];
        }
        else
        {
            $subid = 0;
        }
        $bettype = $betArr[0];
        if ($amount == 0)
        {
            $amountstr = "amount1";
            $amountstr2 = "amount3";
        }
        else
        {
            $amountstr = "amount2";
            $amountstr2 = "amount4";
        }

        $ratemodstr = $ratemod == 1 ? "rate1" : "rate2";
		$sql = $mstFlag ? "!!" : "";
		$sql .= "SELECT totalnum, {$amountstr} as amount, {$amountstr2} as amount_rate, {$ratemodstr} as rate FROM {$this->table_name}";
		$sql .= " WHERE user_id='{$showid}' AND bettype='{$bettype}' AND subid='{$subid}' AND numbers='{$number}' AND lotto_id='{$lotto_id}'";
		$sql .= " LIMIT 1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getDataListByNumSs($lotto_id, $bettype, $numberstr, $showid, $disk = "", $amount = "0", $ratemod = 2, $orderby = "amount_rate", $subid = 0, $xyid = -1)
    {
        $rs = new RecordSet();
        if ($amount == "0")
        {
            $amountstr = "amount1" . $disk;
            $amountstr2 = "amount3" . $disk;
        }
        else
        {
            $amountstr = "amount2" . $disk;
            $amountstr2 = "amount4" . $disk;
        }
        if ($orderby == "amount_rate")
        {
            $orderby = $amountstr2;
        }
        else
        {
            $orderby = $amountstr;
        }

        $ratemodstr = $ratemod == 1 ? "rate1" . $disk : "rate2" . $disk;
        $sql = "SELECT numbers, SUM(totalnum{$disk}) as totalnum, SUM({$amountstr}) as amount, SUM({$amountstr2}) as amount_rate, SUM({$ratemodstr}) as rate, MAX(updatetime) as updatetime FROM {$this->table_name}";
        $sql .= " WHERE lotto_id={$lotto_id} AND user_id={$showid} AND bettype={$bettype} AND subid = {$subid} AND numbers IN ({$numberstr})";
        $sql .= " GROUP BY numbers";
        $sql .= " ORDER BY {$orderby} DESC";
        $rs->open($sql, 0, 20);
        return $rs;
    }

    public function chkTbl($tblName)
    {
        $sql = "SHOW TABLES LIKE '{$tblName}'";
        $result = $this->db->query($sql);
        if (0 < mysql_num_rows($result))
        {
            return true;
        }
        return false;
    }

    public function getPaperOpen($lotto_id)
    {
        $sql = "SELECT open FROM lotto_{$this->tbl} WHERE id={$lotto_id} LIMIT 0,1";
        $rs = getdb($sql);
        if (!$rs->eof)
        {
            $date = $rs->f[0];
            $dateArr = explode("-", $date);
            return $dateArr[0] . $dateArr[1] . $dateArr[2];
        }
        return false;
    }

}

?>